package cn.yywd_123.biliarchiver.account;

import cn.yywd_123.biliarchiver.utils.HttpsResponse;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

public class FavoriteLists {
	public static List<FavoriteList> getLists(int mid) {
		List<FavoriteList> lists = new ArrayList<>();

		JsonArray favoriteListInfos = JsonParser.parseString(HttpsResponse.GET("https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=" + mid)).getAsJsonObject().getAsJsonObject("data").getAsJsonArray("list");
		for (JsonElement element : favoriteListInfos) {
			JsonObject info = element.getAsJsonObject();
			lists.add(new FavoriteList(info.get("title").getAsString(), info.get("id").getAsInt(), info.get("media_count").getAsInt()));
		}

		return lists;
	}

	public static class FavoriteList {
		String title;
		int mlid;
		int mediaCount;

		public FavoriteList(String title, int mlid, int mediaCount) {
			this.title = title;
			this.mlid = mlid;
			this.mediaCount = mediaCount;
		}

		public String getTitle() {
			return title;
		}

		public int getMlid() {
			return mlid;
		}

		public int getMediaCount() {
			return mediaCount;
		}

		public List<String> getVideoBVIds() {
			ArrayList<String> list = new ArrayList<>();

			JsonArray favoriteListInfos = JsonParser.parseString(HttpsResponse.GET("https://api.bilibili.com/x/v3/fav/resource/ids?platform=web&media_id=" + mlid)).getAsJsonObject().getAsJsonArray("data");
			for (JsonElement element : favoriteListInfos) {
				JsonObject info = element.getAsJsonObject();

				if (info.get("type").getAsInt() != 2) continue;

				list.add(info.get("bvid").getAsString());
			}

			return list;
		}

		@Override
		public String toString() {
			return "FavoriteList{" +
					"title='" + title + '\'' +
					", mlid=" + mlid +
					", mediaCount=" + mediaCount +
					'}';
		}
	}
}
